#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef long long LL;
priority_queue<LL, vector<LL>, greater<LL>> hp; 
int n;
LL ret;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		LL x; cin >> x;
		hp.push(x);
	}
	
	while(hp.size() > 1)
	{
		LL x = hp.top(); hp.pop();
		LL y = hp.top(); hp.pop();
		ret += x + y;
		hp.push(x + y);
	}
	
	cout << ret << endl;
	
	return 0;
}
